package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comment> findComments(String movementId, Integer commmentType, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commmentType);

        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page - 1) * pagesize)
                .limit(pagesize);
        return mongoTemplate.find(query,Comment.class);
    }

    //保存互动数据
    public Integer save(Comment comment) {
        //1、设置被评论人和时间
        //根据动态id查询动态
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        comment.setPublishUserId(movement.getUserId());
        comment.setCreated(System.currentTimeMillis());
        //2、保存评论
        mongoTemplate.save(comment);
        //3、更新互动的数量
        Query query = Query.query(Criteria.where("id").is(movement.getId()));
        //更新的字段
        Update update = new Update();
        if(comment.getCommentType() == 1) { //点赞
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == 2){//评论
            update.inc("commentCount",1);
        }else { //喜欢
            update.inc("loveCount",1);
        }
        //更新之后获取最新数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //更新并返回最新的对象
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //4、构造返回值
        if(comment.getCommentType() == 1) { //点赞
            return modify.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            return modify.getCommentCount();
        }else { //喜欢
            return modify.getLoveCount();
        }
    }

    @Override
    public Boolean hasComment(String movementId, Long userId, int commentType) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query,Comment.class); //是否存在 true：存在，false：不存在
    }

    //删除互动数据
    public Integer delete(Comment comment) {
        //1、删除互动数据（根据用户id，互动类型，动态id）
        Criteria criteria = Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //2、更新动态表中的互动数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        //更新的字段
        Update update = new Update();
        if(comment.getCommentType() == 1) { //点赞
            update.inc("likeCount",-1);
        }else if(comment.getCommentType() == 2){//评论
            update.inc("commentCount",-1);
        }else { //喜欢
            update.inc("loveCount",-1);
        }
        //更新之后获取最新数据
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //更新并返回最新的对象
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        //3、删除之后返回总互动数量
        if(comment.getCommentType() == 1) { //点赞
            return modify.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            return modify.getCommentCount();
        }else { //喜欢
            return modify.getLoveCount();
        }
    }
}
